
// 自定义JavaScript - R数据分析平台

$(document).ready(function() {
    // 添加加载动画
    $(document).on('shiny:busy', function(event) {
        $('body').append('<div class="loading"><div class="loading-spinner"></div></div>');
    });
    
    $(document).on('shiny:idle', function(event) {
        $('.loading').remove();
    });
    
    // 平滑滚动到顶部
    $('.sidebar-menu a').click(function() {
        $('html, body').animate({scrollTop: 0}, 'fast');
    });
    
    // 表格行高亮
    $(document).on('mouseenter', 'table.dataTable tbody tr', function() {
        $(this).css('background-color', '#f5f5f5');
    });
    
    $(document).on('mouseleave', 'table.dataTable tbody tr', function() {
        $(this).css('background-color', '');
    });
    
    // 图表容器自适应
    function resizeCharts() {
        $('.echarts4r').each(function() {
            var chart = echarts.getInstanceByDom(this);
            if (chart) {
                chart.resize();
            }
        });
    }
    
    $(window).resize(function() {
        setTimeout(resizeCharts, 100);
    });
    
    // 文件上传反馈
    $('#file').change(function() {
        var fileName = $(this).val().split('\\').pop();
        if (fileName) {
            showNotification('文件 "' + fileName + '" 上传成功', 'success');
        }
    });
    
    // 工具提示
    $('[data-toggle="tooltip"]').tooltip();
    
    // 确认对话框
    window.confirmAction = function(message, callback) {
        if (confirm(message)) {
            callback();
        }
    };
});

// 自定义通知函数
function showNotification(message, type = 'info') {
    var notification = document.createElement('div');
    notification.className = 'custom-notification ' + type;
    notification.innerHTML = message;
    
    document.body.appendChild(notification);
    
    setTimeout(function() {
        notification.classList.add('show');
    }, 100);
    
    setTimeout(function() {
        notification.classList.remove('show');
        setTimeout(function() {
            document.body.removeChild(notification);
        }, 300);
    }, 3000);
}

// 添加CSS样式
var style = document.createElement('style');
style.textContent = `
    .custom-notification {
        position: fixed;
        top: 20px;
        right: 20px;
        padding: 12px 20px;
        border-radius: 4px;
        color: white;
        font-weight: 500;
        z-index: 10000;
        transform: translateX(100%);
        transition: transform 0.3s ease;
        max-width: 300px;
    }
    
    .custom-notification.show {
        transform: translateX(0);
    }
    
    .custom-notification.success {
        background-color: #00a65a;
    }
    
    .custom-notification.error {
        background-color: #dd4b39;
    }
    
    .custom-notification.warning {
        background-color: #f39c12;
    }
    
    .custom-notification.info {
        background-color: #3c8dbc;
    }
`;
document.head.appendChild(style);
